// alert("asdasdasd")

document.getElementById('markdown-input').addEventListener('paste', function (event) {
    const items = (event.clipboardData || window.clipboardData).items;
    for (let i = 0; i < items.length; i++) {
        if (items[i].type.indexOf('image') !== -1) {
            const file = items[i].getAsFile();
            const reader = new FileReader();
            reader.onload = function (event) {
                const base64Data = event.target.result;
                uploadImgFromPaste(base64Data, 'image', true);
            };
            reader.readAsDataURL(file);
        }
    }
});


function uploadImgFromPaste(file, type, isChrome) {
    let url6 = document.location;
    url6.origin;        // http://localhost:8081
    let formData = new FormData();
    formData.append('imgByte', file);
    formData.append('submission-type', type);

    fetch(url6.origin + '/save_img_by_str', {
        method: 'POST',
        body: formData
    })
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.text();
        })
        .then(data => {
            console.log("返回结果:")
            console.log(data)
            // let img_path = url6.origin + "/" + data;
            let img_path = "/" + data;
            console.log(img_path)   // http://192.168.8.194:8065/images\20240707\20240707150210571.png
            //     `${str1} ${str2}`   `![](${img_path})`
            insertTextAtCursor(`![](${img_path})`)
            return img_path;
        })
        .catch(error => {
            console.error('Fetch error:', error);
            insertTextAtCursor('程序发生错误...')
            return "ERROR";
        });
}
